﻿using System;
using System.IO;
using System.Security.Cryptography;
using System.Text;

namespace XiaoSe.Encryption
{
    /// <summary>
    /// 对称加密
    /// </summary>
    public class SymmetricEncryption
    {
        private readonly ICryptoTransform _encryptor;     // 加密器对象
        private readonly ICryptoTransform _decryptor;     // 解密器对象
        private const int BufferSize = 1024;

        /// <summary>
        /// 
        /// </summary>
        /// <param name="key">16位大小写字母与数字的组合</param>
        public SymmetricEncryption(string key)
        {
            SymmetricAlgorithm provider = SymmetricAlgorithm.Create("TripleDES");
            provider.Key = Encoding.UTF8.GetBytes(string.IsNullOrEmpty(key) ? "aBCEdCHIRDsdFVBx" : key);
            provider.IV = new byte[] { 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF };//打散重复,通常IV为64位
            _encryptor = provider.CreateEncryptor();
            _decryptor = provider.CreateDecryptor();
        }
        /// <summary>
        /// 加密字符串
        /// </summary>
        /// <param name="clearText">明文</param>
        /// <returns>密文</returns>
        public string Encrypt(string clearText)
        {
            //创建明文流
            byte[] clearBuffer = Encoding.UTF8.GetBytes(clearText);
            var clearStream = new MemoryStream(clearBuffer);
            //创建密文流
            var encryptedStream = new MemoryStream();
            var cryptoStream = new CryptoStream(encryptedStream, _encryptor, CryptoStreamMode.Write);
            // 将明文流写入到buffer中
            // 将buffer中的数据写入到cryptoStream中
            int bufferRead;
            var buffer = new byte[BufferSize];
            do
            {
                bufferRead = clearStream.Read(buffer, 0, BufferSize);
                cryptoStream.Write(buffer, 0, bufferRead);
            }
            while (bufferRead > 0);
            cryptoStream.FlushFinalBlock();
            // 获取加密后的文本
            buffer = encryptedStream.ToArray();
            return Convert.ToBase64String(buffer);
        }
        /// <summary>
        /// 解密字符串
        /// </summary>
        /// <param name="encryptedText">密文</param>
        /// <returns>明文</returns>
        public string Decrypt(string encryptedText)
        {
            //创建密文流
            byte[] encryptedBuffer = Convert.FromBase64String(encryptedText);
            var encryptedStream = new MemoryStream(encryptedBuffer);
            //创建明文流
            var clearStream = new MemoryStream();
            var cryptoStream = new CryptoStream(encryptedStream, _decryptor, CryptoStreamMode.Read);
            // 将密文流写入到buffer中
            // 将buffer中的数据写入到ClearStream中
            int bufferRead;
            var buffer = new byte[BufferSize];
            do
            {
                bufferRead = cryptoStream.Read(buffer, 0, BufferSize);
                clearStream.Write(buffer, 0, bufferRead);
            }
            while (bufferRead > 0);
            // 获取解密后的文本
            buffer = clearStream.GetBuffer();
            return Encoding.UTF8.GetString(buffer, 0, (int)clearStream.Length);
        }
    }
}
